package com.five.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.five.dto.*;
import com.five.service.UsersService;
import com.five.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController  {


    @Autowired
    private UsersService usersService;



    @PostMapping("/login")
    public SaResult login(@RequestBody UserLoginDto userLoginDto){
        System.out.println(userLoginDto);
        return SaResult.get(200,"登录成功！",1);
//        return usersService.login(userLoginDto);
    }


    @PostMapping("/register")
    public SaResult register(@RequestBody UserRegisterDto userRegisterDto){
        System.out.println(userRegisterDto);
        return usersService.register(userRegisterDto);
    }

    @GetMapping("/userInfo/{username}")
    public SaResult queryUserInfo(@PathVariable("username") String username){
        return  usersService.queryUserInfo(username);
    }

    @GetMapping("/logout")
    public SaResult logout(){
        StpUtil.logout();
        return SaResult.ok("登出成功！");
    }

    @PostMapping("/checkPwd")
    public SaResult checkPwd(@RequestBody UserPasswordDto userPasswordDto){
        return usersService.checkUserPwd(userPasswordDto);
    }

    @PutMapping("/pwd")
    public SaResult updatePwd(@RequestBody UserPasswordDto userPasswordDto){
        return usersService.updateUserPwd(userPasswordDto);
    }

    @PutMapping("/updUserInfo")
    public  SaResult updatUsaerInfo(@RequestBody UserUpdateDto userUpdateDto){
        return usersService.updateUserInfo(userUpdateDto);
    }

    @PostMapping("/checkUserName")
    public SaResult checkPwd(@RequestBody UserRegisterDto userRegisterDto){
        return usersService.checkUserName(userRegisterDto);
    }
    @GetMapping("/users")
    public SaResult getAllUsers(@RequestParam Integer currentPage, @RequestParam Integer pageSize,
                                @RequestParam(required = false) String name) {
        List<UserVo> users = usersService.getAllUsers(currentPage, pageSize, name);
        Long totalUsers = usersService.countAllUsers(name);
        return SaResult.ok().setData(users).set("total", totalUsers);
    }

    @GetMapping("/users/search")
    public SaResult getUserByName(@RequestParam String name) {
        List<UserVo> users = usersService.getUserByName(name);
        return users != null ? SaResult.ok().setData(users) : SaResult.error("未找到用户！");
    }

    @PostMapping("/users")
    public SaResult addUser(@RequestBody UserDto userDto) {
        return usersService.addUser(userDto);
    }

    @DeleteMapping("/users/{id}")
    public SaResult deleteUser(@PathVariable String id) {return usersService.deleteUser(id);}

    @DeleteMapping("/users")
    public SaResult deleteUsers(@RequestBody String[] userIds) {
        return usersService.deleteUsers(userIds);
    }

    @PutMapping("/users")
    public SaResult updateUser(@RequestBody UserDto userDto) {
        // 检查是否能找到用户
        if (userDto.getId() == null || userDto.getId().isEmpty()) {
            return SaResult.error("用户ID不能为空！");
        }
        return usersService.updateUser(userDto);
    }

    //    查询社团团长id
    @GetMapping("/managerids")
    public SaResult querymanagerIdsInfo()
    {
        return usersService.querymanagerIdsInfo();
    }




//    @GetMapping("/activities")
//    public SaResult queryActivitiesInfo(ActivitiesQueryDto activitiesQueryDto){
//        return activitiesService.queryActivitiesInfos(activitiesQueryDto);
//    }
}